﻿@page
@model PrivacyModel
@{
    ViewData["Title"] = "新增任务";
}
<header class='notice-header'>
    <h1 class="notice-title">新增任务</h1>
</header>
<div class="weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label required">任务名称</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="text" id="taskName" placeholder="请输入任务名称">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label required">接收人</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="text" id="Received" value="@@all" placeholder="指定接收人">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label for="" class="weui-label required">日期</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="date" id="date-inline" value="">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label for="time-inline" class="weui-label required">时间</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="time-inline" type="text" value="">
        </div>
    </div>


    <div class="weui-cell weui-cell_select weui-cell_select-after">
        <div class="weui-cell__hd">
            <label for="" class="weui-label">触发类型</label>
        </div>
        <div class="weui-cell__bd">
            <select class="weui-select" id="TaskType" onchange="tasktypechange(this)">
                <option value="0">触发1次</option>
                <option value="1">循环触发</option>
            </select>
        </div>
    </div>
    <div class="weui-cell weui-cell_select weui-cell_select-after hide" id="divTaskRunType">
        <div class="weui-cell__hd">
            <label for="" class="weui-label">循环次数</label>
        </div>
        <div class="weui-cell__bd">
            <select class="weui-select" id="TaskRunType">
                <option value="0">每天</option>
                <option value="1">每周一</option>
                <option value="2">每周二</option>
                <option value="3">每周三</option>
                <option value="4">每周四</option>
                <option value="5">每周五</option>
                <option value="6">每周六</option>
                <option value="7">每周日</option>
            </select>
        </div>
    </div>
    <div class="weui-cell weui-cell_select weui-cell_select-after">
        <div class="weui-cell__hd">
            <label for="" class="weui-label">执行类型</label>
        </div>
        <div class="weui-cell__bd">
            <select class="weui-select" id="TaskStartType">
                <option value="0">直接发送内容</option>
                <option value="1">执行任务</option>
            </select>
        </div>
    </div>
    <div class="weui-cells__title required">消息内容</div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <textarea class="weui-textarea" placeholder="请输入发送的消息内容" rows="2" onkeyup="changetextarea(this)"></textarea>
                <div class="weui-textarea-counter"><span>0</span>/200</div>
            </div>
        </div>
    </div>


    
    <div class="weui-btn-area">
        <a class="weui-btn weui-btn_primary" href="javascript:" id="AddBtn">保 存</a>
        <a href="javascript:window.location.href='/task';" class="weui-btn weui-btn_default">取 消</a>
        <input type="hidden" id="uid" />
    </div>
</div>


@section Scripts{
    <script type="text/javascript">
        $(function () {
            $("#time-inline").picker({
                title: "请选择开始时间",
                cols: [
                    {
                        textAlign: 'center',
                        values: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']

                    },
                    {
                        textAlign: 'center',
                        values: [':']
                    },
                    {
                        textAlign: 'center',
                        values: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59']
                    }
                ]
            });
            var date = new Date();
            $("#date-inline").val(date.DateFormat("yyyy-MM-dd"));
            $("#time-inline").picker("open"); $("#time-inline").picker("close");
            var hour = date.getHours();
            var minute = date.getMinutes() + 5;
            $("#time-inline").picker("setValue", [hour > 9 ? hour : '0' + hour, ":", minute > 9 ? minute : '0' + minute]);


            if (!IsNullOrEmpty(getUrlParam("id"))) {
                var _id = getUrlParam("id");
                $("#uid").val(_id);
                loadData(_id);
            }
        });
       
        $("#AddBtn").click(function () {
            var reg = new RegExp(" ", "g");
            var d = {
                "taskName": $("#taskName").val(),
                "firstDate": $("#date-inline").val() + "T" + $("#time-inline").val().replace(reg, '') + ":00",
                "startDate": $("#date-inline").val(),
                "startHour": $.trim($("#time-inline").val().split(':')[0]),
                "startMinute": $.trim($("#time-inline").val().split(':')[1]),
                "taskType": $("#TaskType").val(),
                "taskRunType": $("#TaskRunType").val(),
                "taskStartType": $("#TaskStartType").val(),
                "content": $(".weui-textarea").val(),
                "status": 0,
                "received": $("#Received").val()
            };

            if (IsNullOrEmpty(d.taskName)) {
                $.toptip("任务名称不为空", 'error');
                return;
            }
            if (IsNullOrEmpty(d.content)) {
                $.toptip("消息内容不为空", 'error');
                return;
            }
            if (IsNullOrEmpty(d.received)) {
                $.toptip("接收人不为空", 'error');
                return;
            }
            if (IsNullOrEmpty(d.startDate)) { 
                $.toptip("日期不为空", 'error');
                return;
            }
            if (IsNullOrEmpty(d.startHour) || IsNullOrEmpty(d.startMinute)) {
                $.toptip("时间不为空", 'error');
                return;
            }
            var url = "/Tasks/Insert";
            if (!IsNullOrEmpty($("#uid").val())) {
                url = "/Tasks/Update";
                d.uid = $("#uid").val();
            } 
            ajax({
                url: url,
                data: d,
                type: "POST",
                success: function (data) { 
                    if (data.type == 200) {
                        $.toptip(data.message, 'success');
                         window.location.href = '/task'
                    } else if (data.type = 404) {
                        $.toptip(data.message, 'error');
                    }
                }

            })
        });

        function tasktypechange(obj) {
            var v = $(obj).val();
            if (v == "1") {
                $("#divTaskRunType").removeClass("hide");
            }
            else
                $("#divTaskRunType").addClass("hide");
        }

        function changetextarea(obj) {
            var v = $(obj).val();
            $(".weui-textarea-counter span").text(v.length);
        }  
       

        function loadData(id) { 
            ajax({
                url: "/Tasks/QueryOne?id="+id,
                type: "GET",
                success: function (data) {
                    var date = new Date(data.firstDate);
                    $("#taskName").val(data.taskName); 
                    $("#date-inline").val(date.DateFormat("yyyy-MM-dd"));
                    $("#time-inline").picker("open"); $("#time-inline").picker("close");
                    var hour = date.getHours();
                    var minute = date.getMinutes();
                    $("#time-inline").picker("setValue", [hour > 9 ? hour : '0' + hour, ":", minute > 9 ? minute : '0' + minute]);
                    $("#TaskType").val(data.taskType);
                    $("#TaskRunType").val(data.taskRunType);
                    $("#TaskStartType").val(data.taskStartType);
                    $(".weui-textarea").val(data.content);
                    $("#Received").val(data.received);
                    $("#_id").val(data._Id);
                }

            })

        }
    </script>
}
